//
//  LSPuerseDetailVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/8/15.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LSPuerseDetailVC.h"
#import "LSPuerseDetailCell.h"
#import "LSPuerseDetailView.h"

static NSString *lSPuerseDetailCell = @"LSPuerseDetailCell";
@interface LSPuerseDetailVC (){

    NSInteger page;
    NSMutableArray *dataArr;
    NSString * statuStr;
    
}

@end

@implementation LSPuerseDetailVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.title = @"明细";
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"明细";
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    self.tableView.backgroundColor = RGBA(247.0, 247.0, 247.0, 1);
    // 注册
    UINib * nib = [UINib nibWithNibName:@"LSPuerseDetailCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:lSPuerseDetailCell];
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 集成上拉加载更多
    [self setupUpRefresh];
    
    dataArr = [NSMutableArray array];
    page = 1;
    [self sendRequest];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setupUpRefresh{

    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    // 设置刷新图片
    UIImage *images = [UIImage imageNamed:@"mj_footer"];
    NSArray *refreshingImages = @[images];
    [footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多内容了" forState:MJRefreshStateRefreshing];
    footer.stateLabel.font = [UIFont systemFontOfSize:12.0f];
    footer.stateLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0f];
    
    [footer setAutomaticallyRefresh:NO];
    
    self.tableView.mj_footer = footer;

}

-(void)loadMoreData{
    
    page++;
    [self sendRequest];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
     [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:dataArr.count];
    return dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

        return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (dataArr.count > 0) {
        
        return 64;
        
    }else{
        
        return screenH;
    }

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 10)];
    headView.backgroundColor = RGBA(247.0, 247.0, 247.0, 1);
    
    return headView;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSPuerseDetailCell * cell = [self.tableView dequeueReusableCellWithIdentifier:lSPuerseDetailCell];
    if (!cell) {
        cell = [[LSPuerseDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lSPuerseDetailCell];
    }

    cell.accessoryType = UITableViewCellAccessoryNone;
    
    NSDictionary * dict = dataArr[indexPath.section];
    
    NSInteger status = [dict[@"payStatus"] integerValue];
    [self status:status];
    
    cell.mainTitle.text = [NSString stringWithFormat:@"%@%@",dict[@"payReason"],statuStr];
    cell.timeTitle.text = dict[@"payTime"];
    
    if ([dict[@"payType"] integerValue] == 2) {
        
        cell.cashTitle.text = [NSString stringWithFormat:@"-%.2f",[dict[@"payMoney"] floatValue]];
        cell.cashTitle.textColor = [UIColor greenColor];
        
    }else{
    
        cell.cashTitle.text = [NSString stringWithFormat:@"+%.2f",[dict[@"payMoney"] floatValue]];
        cell.cashTitle.textColor = [UIColor redColor];
    
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
    NSDictionary * dict = dataArr[indexPath.section];
    
    LSPuerseDetailView * LSPuerseDetailview = [[LSPuerseDetailView alloc]init];
    LSPuerseDetailview.dict = dict;
    [self.navigationController pushViewController:LSPuerseDetailview animated:YES];

}

#pragma mark - 请求数据
-(void)sendRequest{
    NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
    [muDicParameters setObject:allConstMemberToken forKey:@"token"];//用户类型 1-个人，2-企业，3-政府
    [muDicParameters setObject:allConstMemberid forKey:@"userId"];
    [muDicParameters setObject:@(page) forKey:@"page"];

    
    [LSHttpTool sendHttpData:@"getBillList_app_pay.im.lensum" params:muDicParameters success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];

        //        NSString * jsonStr = [LSHttpTool DataTOjsonString:dicInfo];
        //        NSLog(@"--------------------json:%@",jsonStr);
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            if (page == 1) {
                dataArr = nil;
                dataArr = dicInfo[@"data"];
                
                
            }else{
                NSArray *arr = dicInfo[@"data"];
                
                NSMutableArray * mtArr = [NSMutableArray array];
                for (NSDictionary * dict in dataArr) {
                    [mtArr addObject:dict];
                }
                for (NSDictionary * dict in arr) {
                    [mtArr addObject:dict];
                }
                dataArr = nil;
                dataArr = mtArr;
                
            }

            [self.tableView.mj_footer endRefreshing];

            
        }else{

            self.tableView.rowHeight = screenH;
            [self.tableView.mj_footer endRefreshing];

        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];
        
        [self.tableView.mj_footer endRefreshing];

    }];
}

-(NSString *)status:(NSInteger)status{
    
    switch (status) {
        case 1:
            statuStr = @"(处理中)";
            break;
            
        case 2:
            statuStr = @"(处理成功)";
            break;
            
        case 3:
            statuStr = @"(处理失败)";
            break;
            
        default:
            break;
    }

    return statuStr;

}
@end
